<?php

namespace app\controller\api;

use app\BaseController;
use think\facade\Db;
use think\Request;

class IndexController extends BaseController
{
    public function upload(Request $request)
    {
        //获取所有上传的参数
        try {
            $rawInput = $request->getContent();
            $result = json_decode($rawInput, true);

            $uid = $result['uid'];


            $task_info = Db::name("task")->where("uid", $uid)->find();
            if ($task_info) {
                Db::name("task")->where("uid", $uid)->update(['content' => $rawInput, 'update_time' => time()]);
            } else {
                $data = [
                    'uid' => $uid,
                    'content' => $rawInput,
                    'create_time' => time(),
                    'update_time' => time()
                ];
                Db::name("task")->insert($data);
            }
            file_put_contents("/opt/huisheng/wwwroot/api.raisound.com/html/ai_company/log.txt", $uid . PHP_EOL, FILE_APPEND);
            return retJson();
        } catch (\Exception $e) {
            file_put_contents("/opt/huisheng/wwwroot/api.raisound.com/html/ai_company/log.txt", $e->getMessage() . "-----" . $e->getTraceAsString() . "-----" . ($result ?? "") . PHP_EOL, FILE_APPEND);
            return retError($e->getMessage());
        }
    }
}
